package vsion.demo.controller.safe;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import vsion.demo.base.Constants;
import vsion.demo.base.Curruser;
import vsion.demo.base.Result;
import vsion.demo.dto.UserDto;
import vsion.demo.exception.BuesinessException;
import vsion.demo.exception.SysException;
import vsion.demo.service.safty.LoginService;

/**
 * @author Vsion
 * @date 2019/9/28 10:17
 */
@RestController
@RequestMapping("/safty/login")
@SessionAttributes(Constants.SESSION_ATTR_CURR_USER)//说明什么属性存储于session中
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("/user")
    public Result login(@RequestBody UserDto dto ,Model model){
        try {
            //返回当前用户
            Curruser curruser=  loginService.checkLogin(dto);
            //model默认存放在request中
            model.addAttribute(Constants.SESSION_ATTR_CURR_USER,curruser);
            return Result.success("登录成功");
        } catch (BuesinessException e) {
            return Result.fail(e.getMessage());
        }catch (SysException e){
            e.printStackTrace();
            return Result.fail("系统繁忙...");
        }

    }
}
